home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 August: Technology Seed / ADC Seed CD - August 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / SimpleText / ThreeDMetafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-04  |  12.8 KB  |  425 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ThreeDMetafile.c
  3.  
  4.     Contains:    3D viewer support for SimpleText
  5.  
  6.     Version:    SimpleText 1.4 or later
  7.  
  8.     Written by:    Eiichiro Mikami, Tom Dowdy
  9.  
  10.     Copyright:    © 1994-1997 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     File Ownership:
  13.  
  14.         DRI:                Tom Dowdy
  15.  
  16.         Other Contact:        Jim Negrette
  17.  
  18.         Technology:            Macintosh Graphics Group
  19.  
  20.     Writers:
  21.  
  22.         (dmp)    Dave Polaschek
  23.         (ted)    Tom Dowdy
  24.         (ecs)    Eric Schlegel
  25.         (TD)    Tom Dowdy
  26.  
  27.     Change History (most recent first):
  28.  
  29.     $Log: ThreeDMetafile.c,v $
  30.     Revision 1.2  1998/11/19 01:16:21  wilkes
  31.     Removed reliance on QuickTime.  Most changes were conditionalized with
  32.     ALLOW_QUICKTIME...
  33.  
  34.     Revision 1.1.1.1  1998/03/18 22:56:12  ivory
  35.     Initial checkin of SimpleText.
  36.  
  37.         
  38.         2     7/29/97 2:05 PM Tom Dowdy
  39.         Removed all of the old and boring refs
  40.         
  41.         1     7/28/97 11:25 AM Duane Byram
  42.         first added to Source Safe project
  43.  
  44.         <11>    11/26/96    ecs        smarter cursor adjustment
  45.         <10>    11/25/96    ted        [1604457]  Fixing 3d crashing in FAT version.
  46.          <9>      9/9/96    dmp        staticfy local functions to eliminate warnings in MWC.
  47.          <8>     7/30/96    ted        adding inactive button support
  48.          <7>     5/31/96    ted        removing unused local
  49.          <6>      2/8/96    ted        Fixing SC compiler problems
  50.          <5>      2/8/96    ted        Fixing SC compiler problems
  51.          <4>      1/4/96    ecs        compile w/ CodeWarrior
  52.          <3>     10/2/95    TD        adding support for SC compiler
  53.          <2>     9/13/95    TD        adding commas in INIT portion
  54.          <1>     8/21/95    TD        First checked in.
  55.  
  56. */
  57.  
  58.  
  59.  
  60. #include "MacIncludes.h"
  61. #include <String.h>
  62. #include "ThreeDMetafile.h"
  63.  
  64.  
  65. #if ALLOW_QUICKTIME
  66. static GlobalEntry    gProcPointers[] =
  67.     {
  68.     {uppViewerLibNewProcInfo, nil},
  69.     {uppViewerLibDisposeProcInfo, nil},
  70.  
  71.     {uppViewerLibUseFileProcInfo, nil},
  72.     {uppViewerLibUseDataProcInfo, nil},
  73.     
  74.     {uppViewerLibWriteFileProcInfo, nil},
  75.     {uppViewerLibWriteDataProcInfo, nil},
  76.     
  77.     {uppViewerLibDrawProcInfo, nil},
  78.  
  79.     {uppViewerLibGetPictProcInfo, nil},
  80.     {uppViewerLibGetButtonRectProcInfo, nil},
  81.  
  82.     {uppViewerLibGetDimensionProcInfo, nil},
  83.  
  84.     {uppViewerLibSetFlagsProcInfo, nil},
  85.     {uppViewerLibGetFlagsProcInfo, nil},
  86.  
  87.     {uppViewerLibSetBoundsProcInfo, nil},
  88.     {uppViewerLibGetBoundsProcInfo, nil},
  89.  
  90.     {uppViewerLibSetPortProcInfo, nil},
  91.     {uppViewerLibGetPortProcInfo, nil},
  92.  
  93.     {uppViewerLibEventProcInfo, nil},
  94.     {uppViewerLibAdjustCursorProcInfo, nil},
  95.  
  96.     {uppViewerLibGetStateProcInfo, nil},
  97.  
  98.     {uppViewerLibClearProcInfo, nil},
  99.     {uppViewerLibCutProcInfo, nil},
  100.     {uppViewerLibCopyProcInfo, nil},
  101.     {uppViewerLibPasteProcInfo, nil}
  102.     };    
  103. #endif
  104.     
  105.  
  106. // --------------------------------------------------------------------------------------------------------------
  107. // PRIVATE ROUTINES
  108. // --------------------------------------------------------------------------------------------------------------
  109. static UniversalProcPtr MakeARoutineDescriptor(void *theProc, ProcInfoType theProcInfo)
  110. /*
  111.     The Symantec compiler doesn't like jumping into staticly done routine descriptors,
  112.     so we have to actually allocate them as pointers.  Sigh.
  113. */
  114. {
  115.     RoutineDescriptor rd = BUILD_ROUTINE_DESCRIPTOR(0, nil);
  116.     
  117.     Ptr    newPtr;
  118.     
  119.     rd.routineRecords[0].procInfo = theProcInfo;
  120.     rd.routineRecords[0].procDescriptor = theProc;
  121.     rd.routineRecords[0].ISA = kPowerPCISA + kPowerPCRTA;
  122.     newPtr = NewPtrClear(sizeof(rd));
  123.     if (newPtr)
  124.         BlockMove(&rd, newPtr, sizeof(rd));
  125.     return((UniversalProcPtr) newPtr);
  126.     
  127. } // MakeARoutineDescriptor
  128.  
  129. // --------------------------------------------------------------------------------------------------------------
  130. static void FillInEntryPoints(void)
  131. {
  132. #if ALLOW_QUICKTIME
  133.     long                result;
  134.     
  135.     if ( 
  136.         (Gestalt('qd3d', &result) == noErr) && 
  137.         (Gestalt('sysa', &result) == noErr) && (result == 2) 
  138.         )
  139.         {
  140.         CFragSymbolClass    symClass;
  141.         CFragConnectionID    connID;
  142.         Ptr                 mainAddr;
  143.         Str255                errName;
  144.         long                procID = 0;
  145.         
  146.         if (GetSharedLibrary("\pQD3DViewerLib", 'pwpc', 1, &connID, &mainAddr, errName) == noErr)
  147.             {
  148.             FindSymbol(connID, "\pQ3ViewerNew", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  149.             FindSymbol(connID, "\pQ3ViewerDispose", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  150.             
  151.             FindSymbol(connID, "\pQ3ViewerUseFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  152.             FindSymbol(connID, "\pQ3ViewerUseData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  153.  
  154.             FindSymbol(connID, "\pQ3ViewerWriteFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  155.             FindSymbol(connID, "\pQ3ViewerWriteData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  156.  
  157.             FindSymbol(connID, "\pQ3ViewerDraw", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  158.  
  159.             FindSymbol(connID, "\pQ3ViewerGetPict", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  160.             FindSymbol(connID, "\pQ3ViewerGetButtonRect", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  161.  
  162.             FindSymbol(connID, "\pQ3ViewerGetDimension", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  163.  
  164.             FindSymbol(connID, "\pQ3ViewerSetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  165.             FindSymbol(connID, "\pQ3ViewerGetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  166.  
  167.             FindSymbol(connID, "\pQ3ViewerSetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  168.             FindSymbol(connID, "\pQ3ViewerGetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  169.  
  170.             FindSymbol(connID, "\pQ3ViewerSetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  171.             FindSymbol(connID, "\pQ3ViewerGetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  172.  
  173.             FindSymbol(connID, "\pQ3ViewerEvent", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  174.             FindSymbol(connID, "\pQ3ViewerAdjustCursor", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  175.  
  176.             FindSymbol(connID, "\pQ3ViewerGetState", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  177.  
  178.             FindSymbol(connID, "\pQ3ViewerClear", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  179.             FindSymbol(connID, "\pQ3ViewerCut", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  180.             FindSymbol(connID, "\pQ3ViewerCopy", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  181.             FindSymbol(connID, "\pQ3ViewerPaste", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  182.             }
  183.         
  184.         // build the routine descriptors
  185.         {
  186.         short    i;
  187.         
  188.         for (i = 0; i < procID; ++i)
  189.             gProcPointers[i].entry = MakeARoutineDescriptor(gProcPointers[i].entry, gProcPointers[i].info);
  190.         }
  191.         
  192.         // as long as it isn't NIL, we're OK
  193.         if (gProcPointers[0].entry)
  194.             gMachineInfo.haveThreeD = true;
  195.         }
  196. #endif
  197. } // FillInEntryPoints
  198.  
  199. // --------------------------------------------------------------------------------------------------------------
  200. // OOP INTERFACE ROUTINES
  201. // --------------------------------------------------------------------------------------------------------------
  202.  
  203. static OSErr ThreeDCloseWindow(
  204.             WindowPtr pWindow, 
  205.             WindowDataPtr pData)
  206. {
  207. #pragma unused (pWindow)
  208.  
  209. #if ALLOW_QUICKTIME
  210.     ViewerLib_Dispose( ((ThreeDDataPtr)pData)->viewerObject);
  211. #endif
  212.  
  213.     return(noErr);
  214.     
  215. } // ThreeDCloseWindow
  216.  
  217. // --------------------------------------------------------------------------------------------------------------
  218. static OSErr ThreeDGetBalloon(
  219.             WindowPtr pWindow, 
  220.             WindowDataPtr pData, 
  221.             Point *localMouse,
  222.             short * returnedBalloonIndex, 
  223.             Rect *returnedRectangle)
  224. {
  225. #pragma unused (pWindow, pData, localMouse, returnedBalloonIndex, returnedRectangle)
  226.  
  227.     *returnedBalloonIndex = iDidTheBalloon;
  228.     
  229.     return(noErr);
  230.     
  231. } // ThreeDGetBalloon
  232.  
  233. // --------------------------------------------------------------------------------------------------------------
  234. static Boolean    ThreeDFilterEvent(
  235.             WindowPtr pWindow, 
  236.             WindowDataPtr pData, 
  237.             EventRecord *pEvent)
  238. {
  239. #pragma unused (pWindow)
  240.     
  241. #if ALLOW_QUICKTIME
  242.     if (pEvent->what == activateEvt)
  243.         {
  244.         if (pEvent->modifiers & activeFlag)
  245.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) | kQ3ViewerActive);
  246.         else
  247.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) & ~kQ3ViewerActive);
  248.         }
  249.         
  250.     return((Boolean)ViewerLib_Event (((ThreeDDataPtr)pData)->viewerObject, pEvent));
  251. #endif
  252.     return false;
  253.     
  254. } // ThreeDFilterEvent
  255.  
  256.  
  257. // --------------------------------------------------------------------------------------------------------------
  258. static OSErr ThreeDAdjustCursor(WindowPtr pWindow, WindowDataPtr pData, Point *localMouse, RgnHandle globalRgn)
  259. {
  260. #pragma unused (pWindow)
  261.  
  262.     OSErr anErr = noErr;
  263.  
  264. #if ALLOW_QUICKTIME
  265.     ViewerLib_AdjustCursor(((ThreeDDataPtr)pData)->viewerObject, localMouse);
  266.     
  267.     // since we don't know how the Viewer will adjust the cursor, make sure we get continuous calls
  268.     SetEmptyRgn(globalRgn);
  269. #endif
  270.     
  271.     return(eActionAlreadyHandled);
  272.     
  273. } // ThreeDAdjustCursor
  274.  
  275. // --------------------------------------------------------------------------------------------------------------
  276. static OSErr ThreeDGetCoachRectangle(WindowPtr pWindow, WindowDataPtr pData, Rect *pRect, Ptr name)
  277. {
  278. #pragma unused (pWindow)
  279.  
  280.     OSErr anErr = fnfErr;
  281.     
  282. #if ALLOW_QUICKTIME
  283.     if (strcmp(name, "Camera") == 0)
  284.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonCamera, pRect);
  285.     if (strcmp(name, "Truck") == 0)
  286.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonTruck, pRect);
  287.     if (strcmp(name, "Orbit") == 0)
  288.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonOrbit, pRect);
  289.     if (strcmp(name, "Zoom") == 0)
  290.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonZoom, pRect);
  291.     if (strcmp(name, "Dolly") == 0)
  292.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonDolly, pRect);
  293. #endif
  294.             
  295.     return(anErr);
  296.     
  297. } // ThreeDGetCoachRectangle
  298.  
  299. // --------------------------------------------------------------------------------------------------------------
  300. void ThreeDGetFileTypes(
  301.         OSType * pFileTypes,
  302.         OSType * pDocumentTypes,
  303.         short * numTypes)
  304. {
  305. #if ALLOW_QUICKTIME
  306.     if (!gMachineInfo.haveThreeD)
  307.         FillInEntryPoints();
  308.         
  309.     if (gMachineInfo.haveThreeD)
  310.         {
  311.         pFileTypes[*numTypes]         = '3DMF';
  312.         pDocumentTypes[*numTypes]     = kThreeDWindow;
  313.         (*numTypes)++;
  314.         }
  315. #endif
  316. } // ThreeDGetFileTypes
  317.  
  318. // --------------------------------------------------------------------------------------------------------------
  319. static OSErr    ThreeDUpdateWindow(WindowPtr pWindow, WindowDataPtr pData)
  320. {
  321. #pragma unused (pWindow)
  322.  
  323. #if ALLOW_QUICKTIME
  324.     ViewerLib_Draw(((ThreeDDataPtr)pData)->viewerObject);
  325. #endif
  326.     
  327.     return(noErr);
  328.     
  329. } // ThreeDUpdateWindow
  330.  
  331. // --------------------------------------------------------------------------------------------------------------
  332. static OSErr    ThreeDAdjustMenus(WindowPtr pWindow, WindowDataPtr pData)
  333. {
  334. #pragma unused (pWindow)
  335.  
  336.     OSErr anErr = noErr;
  337.     
  338. #if ALLOW_QUICKTIME
  339.     if (ViewerLib_GetState(((ThreeDDataPtr)pData)->viewerObject) ) 
  340.         EnableCommand(cCopy);
  341. #endif
  342.  
  343.     return(anErr);
  344.     
  345. } // ThreeDAdjustMenus
  346.  
  347. // --------------------------------------------------------------------------------------------------------------
  348. static OSErr    ThreeDCommand(WindowPtr pWindow, WindowDataPtr pData, short commandID, long menuResult)
  349. {
  350. #pragma unused (menuResult)
  351.  
  352.     OSErr                        anErr = noErr;
  353. #if ALLOW_QUICKTIME
  354.     ViewerObject                 theViewer = ((ThreeDDataPtr)pData)->viewerObject;
  355.     
  356.     SetPort((GrafPtr) GetWindowPort(pWindow));
  357.         
  358.     switch (commandID) 
  359.         {
  360.         case cCopy:
  361.             anErr = ViewerLib_Copy(theViewer);
  362.             if (anErr == noErr)
  363.                 anErr = eActionAlreadyHandled;
  364.             break;
  365.         } // switch (commandID)
  366. #endif
  367.     return(anErr);
  368.     
  369. } // ThreeDCommand
  370.  
  371. // --------------------------------------------------------------------------------------------------------------
  372. static OSErr    ThreeDMakeWindow(
  373.             WindowPtr pWindow,
  374.             WindowDataPtr pData)
  375. {
  376.     OSErr                        anErr = noErr;
  377. #if ALLOW_QUICKTIME
  378.     ViewerObject                viewerObj;
  379.  
  380.     pData->pCloseWindow         = (CloseWindowProc)            ThreeDCloseWindow;
  381.     pData->pFilterEvent         = (FilterEventProc)            ThreeDFilterEvent;
  382.     pData->pGetBalloon             = (GetBalloonProc)            ThreeDGetBalloon;
  383.     pData->pUpdateWindow         = (UpdateWindowProc)        ThreeDUpdateWindow;
  384.     pData->pAdjustMenus         = (AdjustMenusProc)            ThreeDAdjustMenus;
  385.     pData->pCommand                 = (CommandProc)                ThreeDCommand;
  386.     pData->pAdjustCursor         = (AdjustCursorProc)        ThreeDAdjustCursor;
  387.     pData->pGetCoachRectangle    = (GetCoachRectangleProc)    ThreeDGetCoachRectangle;
  388.  
  389.     pData->dragWindowAligned    = 0;
  390.     
  391.     viewerObj = ViewerLib_New((CGrafPtr )pWindow,  &pData->contentRect,  kQ3ViewerDefault | kQ3ViewerDraggingOff); 
  392.  
  393.     if (viewerObj == 0)
  394.         {
  395.         anErr = memFullErr;
  396.         }
  397.     else
  398.         {
  399.         anErr = ViewerLib_UseFile(viewerObj, pData->dataRefNum);
  400.         if (anErr == noErr)
  401.             ((ThreeDDataPtr)pData)->viewerObject = viewerObj;
  402.         else
  403.             ViewerLib_Dispose(viewerObj);
  404.         
  405.         }
  406. #endif
  407.     
  408.     return(anErr);
  409.     
  410. } // ThreeDMakeWindow
  411.  
  412. // --------------------------------------------------------------------------------------------------------------
  413. OSErr    ThreeDPreflightWindow(PreflightPtr pPreflightData)
  414. {    
  415. #if ALLOW_QUICKTIME
  416.     pPreflightData->continueWithOpen     = true;
  417.     pPreflightData->makeProcPtr         = ThreeDMakeWindow;
  418.     pPreflightData->resourceID            = kThreeDWindowID;
  419.     pPreflightData->storageSize         = sizeof(ThreeDDataRecord);
  420. #endif
  421.  
  422.     return(noErr);
  423.     
  424. } // ThreeDPreflightWindow
  425.